/*通过苹果笔记本电脑的构造理解建造者模式*/

#include <iostream>
#include <string>
#include <memory>

class Computer{
public:
    Computer(){}

    void setBoard(const std::string& board)
    {
        _board = board;
    }

    void setDisplay(const std::string& display)
    {
        _display = display;
    }

    void showParameters()
    {
        std::string parameter = "Computer Parameters:\n";
        parameter += "\tBoard: " + _board + "\n";
        parameter += "\tDisplay: " + _display + "\n";
        parameter += "\tOS: " + _os + "\n";
        std::cout << parameter;
    }

    virtual void setOS() = 0;
// 保证子类在继承之后能够使用下面这些成员变量
protected:
    std::string _board;
    std::string _display;
    std::string _os;
};

class Macbook : public Computer{
public:
    void setOS() override{
        _os = "Mac OS X12";
    }
};

class Builder{
public:
    virtual void buildBoard(const std::string &board) = 0;
    virtual void buildDisplay(const std::string &display) = 0;
    virtual void buildOS() = 0;
    virtual std::shared_ptr<Computer> build() = 0;
};

class MacbookBuilder : public Builder{
public:
    MacbookBuilder()
        :_computer(new Macbook())
    {}

    void buildBoard(const std::string &board) {
        _computer->setBoard(board);
    }

    void buildDisplay(const std::string &display) {
        _computer->setDisplay(display);
    }

    void buildOS()
    {
        _computer->setOS();
    }

    std::shared_ptr<Computer> build(){
        return _computer;
    }
private:
    std::shared_ptr<Computer> _computer;
};

class Director{
public:
    Director(Builder* builder)
        :_builder(builder)
    {}

    void construct(const std::string &board, const std::string &display)
    {
        _builder->buildBoard(board);
        _builder->buildDisplay(display);
        _builder->buildOS();
    }
private:
    std::shared_ptr<Builder> _builder;
};

int main()
{
    Builder *builder = new MacbookBuilder();
    std::unique_ptr<Director> director(new Director(builder));
    director->construct("华硕主板", "三星显示器");
    std::shared_ptr<Computer> computer = builder->build();
    computer->showParameters();

    return 0;
}
